package com.geostar.constructionland.analyse.validtor;

import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Comparator;
import java.util.List;

/**
 * @Author makoto_zhu
 * @Date 2020-11-13 03:43:21
 * @Description 分析模型参数验证
 **/
@Slf4j
public class AlyValid implements ConstraintValidator<ValidAnalyseModelRule, AnalyseModel> {
    private List<IValid> validList;

    public AlyValid(List<IValid> list) {
        this.validList = list;
    }

    @Override
    public boolean isValid(AnalyseModel analyseModel, ConstraintValidatorContext constraintValidatorContext) {
        // 将所有验证器按照自定义顺序排序，然后依次执行验证方法
        validList.stream()
                .sorted(Comparator.comparingInt(IValid::getSort))
                .forEach(validator -> {
                    validator.valid(analyseModel);
                });
        return true;
    }
}
